from repository.SQLiteDatabase import SQLiteDatabase
import json


class SampleRepository:
    def __init__(self):
        with open("config.json", "r") as file:
            config = json.load(file)
        self.db = SQLiteDatabase(config["database"]["dbName"])

    def addSample(self, sampleDO):
        tar = sampleDO.DO2Dict()
        tar.pop("id")
        return self.db.insert_data("sample", tar)

    def querySampleListByUser(self, userId, limit=None, offset=None):
        return self.db.select_data(
            "sample", "operator_id = {}".format(userId), limit, offset
        )

    def querySampleListByFileName(self, name, limit=None, offset=None):
        return self.db.select_data(
            "sample", "origin_name = '{}'".format(name), limit, offset
        )

    def querySampleListByMd5(self, name, limit=None, offset=None):
        return self.db.select_data("sample", "md5 = '{}'".format(name), limit, offset)

    def querySampleSum(self):
        return self.db.runSql("select count(*) from sample")

    def queryTodayUploadSampleSum(self):
        return self.db.runSql(
            "SELECT COUNT(*) FROM sample WHERE DATE(upload_time) = DATE('now') AND is_delete = 0;"
        )

    def delSample(self):
        pass

    def queryDashBoardInf(self):
        pass

    def querySample(self):
        pass

    def close(self):
        self.db.close()
